package com.haoshuang.zhouzhoubang.ui.activity

import android.content.Context
import android.content.res.AssetManager
import android.media.MediaPlayer
import android.os.Bundle
import android.util.Log
import com.haoshuang.zhouzhoubang.config.Config
import com.haoshuang.zhouzhoubang.databinding.ActivityNoticeSettingsBinding
import com.haoshuang.zhouzhoubang.ui.vm.NoticeSettingsViewModel
import com.lnkj.jialubao.utils.AccountUtils
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.view.clickWithTrigger
import java.io.IOException


/**
 *  手机通知设置
 */
class NoticeSettingsActivity :
    BaseVMActivity<NoticeSettingsViewModel, ActivityNoticeSettingsBinding>() {

    companion object {
        fun launch(context: Context) {
            context.startPage<NoticeSettingsActivity>()
        }
    }

    private val player = MediaPlayer()


    override fun initView(savedInstanceState: Bundle?) {

        binding.titleBar.title = "手机通知设置"
        binding.titleBar.leftImageView().click { finish() }

        val newOrderNotice = AccountUtils.newOrderNotice
        val assignOrderNotice = AccountUtils.assignOrderNotice
        val finishOrderNotice = AccountUtils.finishOrderNotice
        val timeoutOrderNotice = AccountUtils.timeoutOrderNotice
        val errorOrderNotice = AccountUtils.errorOrderNotice

        // 设置新订单提醒
        binding.includeNewOrder.tvTitle.text = "新订单提醒"
        binding.includeNewOrder.tvNotice1.text = "周周家服来新订单了"
        binding.includeNewOrder.ivNotice1.isSelected = newOrderNotice == Config.newOrderNotice
        binding.includeNewOrder.ivNotice2.isSelected = newOrderNotice == Config.defaultNotice
        binding.includeNewOrder.llNotice1.clickWithTrigger {
            if (binding.includeNewOrder.ivNotice1.isSelected) return@clickWithTrigger
            AccountUtils.newOrderNotice = Config.newOrderNotice
            binding.includeNewOrder.ivNotice1.isSelected = true
            binding.includeNewOrder.ivNotice2.isSelected = false
            playNotice(AccountUtils.newOrderNotice)
        }
        binding.includeNewOrder.llNotice2.clickWithTrigger {
            if (binding.includeNewOrder.ivNotice2.isSelected) return@clickWithTrigger
            AccountUtils.newOrderNotice = Config.defaultNotice
            binding.includeNewOrder.ivNotice1.isSelected = false
            binding.includeNewOrder.ivNotice2.isSelected = true
            playNotice(AccountUtils.newOrderNotice)
        }

        binding.includeAssignOrder.tvTitle.text = "指派提醒"
        binding.includeAssignOrder.tvNotice1.text = "您有一个订单被指派, 请及时处理"
        binding.includeAssignOrder.ivNotice1.isSelected = assignOrderNotice == Config.assignOrderNotice
        binding.includeAssignOrder.ivNotice2.isSelected = assignOrderNotice == Config.defaultNotice
        binding.includeAssignOrder.llNotice1.clickWithTrigger {
            if (binding.includeAssignOrder.ivNotice1.isSelected) return@clickWithTrigger
            AccountUtils.assignOrderNotice = Config.assignOrderNotice
            binding.includeAssignOrder.ivNotice1.isSelected = true
            binding.includeAssignOrder.ivNotice2.isSelected = false
            playNotice(AccountUtils.assignOrderNotice)
        }
        binding.includeAssignOrder.llNotice2.clickWithTrigger {
            if (binding.includeAssignOrder.ivNotice2.isSelected) return@clickWithTrigger
            AccountUtils.assignOrderNotice = Config.defaultNotice
            binding.includeAssignOrder.ivNotice1.isSelected = false
            binding.includeAssignOrder.ivNotice2.isSelected = true
            playNotice(AccountUtils.assignOrderNotice)
        }

        binding.includeFinishOrder.tvTitle.text = "完工提醒"
        binding.includeFinishOrder.tvNotice1.text = "距离服务结束还有15分钟"
        binding.includeFinishOrder.ivNotice1.isSelected = finishOrderNotice == Config.finishOrderNotice
        binding.includeFinishOrder.ivNotice2.isSelected = finishOrderNotice == Config.defaultNotice
        binding.includeFinishOrder.llNotice1.clickWithTrigger {
            if (binding.includeFinishOrder.ivNotice1.isSelected) return@clickWithTrigger
            AccountUtils.finishOrderNotice = Config.finishOrderNotice
            binding.includeFinishOrder.ivNotice1.isSelected = true
            binding.includeFinishOrder.ivNotice2.isSelected = false
            playNotice(AccountUtils.finishOrderNotice)
        }
        binding.includeFinishOrder.llNotice2.clickWithTrigger {
            if (binding.includeFinishOrder.ivNotice2.isSelected) return@clickWithTrigger
            AccountUtils.finishOrderNotice = Config.defaultNotice
            binding.includeFinishOrder.ivNotice1.isSelected = false
            binding.includeFinishOrder.ivNotice2.isSelected = true
            playNotice(AccountUtils.finishOrderNotice)
        }

        binding.includeTimeoutOrder.tvTitle.text = "超时提醒"
        binding.includeTimeoutOrder.tvNotice1.text = "您有一个订单上门即将超时，请及时处理"
        binding.includeTimeoutOrder.ivNotice1.isSelected = timeoutOrderNotice == Config.timeoutOrderNotice
        binding.includeTimeoutOrder.ivNotice2.isSelected = timeoutOrderNotice == Config.defaultNotice
        binding.includeTimeoutOrder.llNotice1.clickWithTrigger {
            if (binding.includeTimeoutOrder.ivNotice1.isSelected) return@clickWithTrigger
            AccountUtils.timeoutOrderNotice = Config.timeoutOrderNotice
            binding.includeTimeoutOrder.ivNotice1.isSelected = true
            binding.includeTimeoutOrder.ivNotice2.isSelected = false
            playNotice(AccountUtils.timeoutOrderNotice)
        }
        binding.includeTimeoutOrder.llNotice2.clickWithTrigger {
            if (binding.includeTimeoutOrder.ivNotice2.isSelected) return@clickWithTrigger
            AccountUtils.timeoutOrderNotice = Config.defaultNotice
            binding.includeTimeoutOrder.ivNotice1.isSelected = false
            binding.includeTimeoutOrder.ivNotice2.isSelected = true
            playNotice(AccountUtils.timeoutOrderNotice)
        }

        binding.includeErrorOrder.tvTitle.text = "异常提醒"
        binding.includeErrorOrder.tvNotice1.text = "您有一个订单出现异常，请及时处理"
        binding.includeErrorOrder.ivNotice1.isSelected = errorOrderNotice == Config.errorOrderNotice
        binding.includeErrorOrder.ivNotice2.isSelected = errorOrderNotice == Config.defaultNotice
        binding.includeErrorOrder.llNotice1.clickWithTrigger {
            if (binding.includeErrorOrder.ivNotice1.isSelected) return@clickWithTrigger
            AccountUtils.errorOrderNotice = Config.errorOrderNotice
            binding.includeErrorOrder.ivNotice1.isSelected = true
            binding.includeErrorOrder.ivNotice2.isSelected = false
            playNotice(AccountUtils.errorOrderNotice)
        }
        binding.includeErrorOrder.llNotice2.clickWithTrigger {
            if (binding.includeErrorOrder.ivNotice2.isSelected) return@clickWithTrigger
            AccountUtils.errorOrderNotice = Config.defaultNotice
            binding.includeErrorOrder.ivNotice1.isSelected = false
            binding.includeErrorOrder.ivNotice2.isSelected = true
            playNotice(AccountUtils.errorOrderNotice)
        }

    }

    private fun playNotice(path: String){
        if (player.isPlaying){
            player.stop()
        }
        player.reset()
        val assetManager: AssetManager = resources.assets
        try {
            val fileDescriptor = assetManager.openFd(path)
            player.setDataSource(
                fileDescriptor.fileDescriptor,
                fileDescriptor.startOffset,
                fileDescriptor.length
            )
            player.prepare()
            player.start()
        } catch (e: IOException) {
            e.printStackTrace()
        }
    }

    override fun initData() {

    }

    override fun startObserve() {

    }

}